package com.example.srpingbootapi.controller;


import com.example.srpingbootapi.entity.Customer;
import com.example.srpingbootapi.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/test")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @PostMapping("/")
    @ResponseStatus(code = HttpStatus.CREATED)
    public Customer save(@RequestBody Customer customer){
        return customerService.save(customer);
    }

    @PostMapping("/all")
    @ResponseStatus(code = HttpStatus.CREATED)

    public List<Customer> saveAll(@RequestBody List<Customer> customers){
        return customerService.saveAll(customers);
    }

    @GetMapping("/")
    public Page<Customer> findAll(Pageable pageable){
        return customerService.findAll(pageable);
    }

    @GetMapping(value = "/{customerId}")
    public Customer findByCustomerId(@PathVariable Long customerId){
        return customerService.findByCustomerId(customerId);
    }
    // 分页查询
    @GetMapping("/page")
    public List<Customer> getCustomerByPage(int pageNo, int size){
        return customerService.getCustomerByPage(pageNo, size);
    }

    @DeleteMapping(value = "/{customerId}")
    public ResponseEntity<?> deleteCustomer(@PathVariable Long customerId){
        return customerService.deleteCustomer(customerId);
    }

    @PutMapping(value = "/{customerId}")
    public ResponseEntity<Customer> updateCustomer(@PathVariable long customerId, @RequestBody Customer newCustomer){
        return customerService.updateCustomer(customerId, newCustomer);
    }




}
